/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.props.BladeProperties;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.AppExcel;
import org.springblade.modules.app.service.IAppExcelService;
import org.springblade.modules.app.vo.AppExcelVO;
import org.springblade.modules.app.wrapper.AppExcelWrapper;
import org.springblade.modules.system.service.IDictService;
import org.springblade.modules.util.PoiUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器
 *
 * @author Blade
 * @since 2019-07-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app/appexcel")
@Api(value = "", tags = "接口")
public class AppExcelController extends BladeController {
	private BladeProperties bladeProperties;
	private IAppExcelService appExcelService;

	private IDictService dictService;

	@GetMapping("/excel")
	public R excel(String path) {
		String uploadPath = bladeProperties.getProp().get("upload-domain") + "/images";
		System.out.println(uploadPath);
		System.out.println("path:" + path);
		String remotePath = bladeProperties.getProp().get("remote-path");
		System.out.println("path:" + remotePath);
		System.out.println(remotePath + path.replace(uploadPath.trim(), ""));
		try {
			PoiUtil.readExcel(remotePath + path.replace(uploadPath.trim(), ""), appExcelService);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return R.fail("上传失败 ");
		}
		return R.success("上传成功");
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入appExcel", position = 1)
	public R<AppExcelVO> detail(AppExcel appExcel) {
		AppExcel detail = appExcelService.getOne(Condition.getQueryWrapper(appExcel));
		AppExcelWrapper appExcelWrapper = new AppExcelWrapper(dictService);
		return R.data(appExcelWrapper.entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页", notes = "传入appExcel", position = 2)
	public R<IPage<AppExcelVO>> list(AppExcel appExcel, Query query) {
		IPage<AppExcel> pages = appExcelService.page(Condition.getPage(query), Condition.getQueryWrapper(appExcel));
		AppExcelWrapper appExcelWrapper = new AppExcelWrapper(dictService);
		return R.data(appExcelWrapper.pageVO(pages));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入appExcel", position = 3)
	public R<IPage<AppExcelVO>> page(AppExcelVO appExcel, Query query) {
		IPage<AppExcelVO> pages = appExcelService.selectAppExcelPage(Condition.getPage(query), appExcel);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入appExcel", position = 4)
	public R save(@Valid @RequestBody AppExcel appExcel) {
		return R.status(appExcelService.save(appExcel));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入appExcel", position = 5)
	public R update(@Valid @RequestBody AppExcel appExcel) {
		return R.status(appExcelService.updateById(appExcel));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入appExcel", position = 6)
	public R submit(@Valid @RequestBody AppExcel appExcel) {
		return R.status(appExcelService.saveOrUpdate(appExcel));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids", position = 7)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(appExcelService.removeByIds(Func.toIntList(ids)));
	}


}
